var btn_submitImage = document.querySelector('#submitImage');
var input_imageFile = document.querySelector('#imageFile');
var image_portrait = document.querySelector('#portrait');
var btn_update_info =document.querySelector('#update_info');
var btn_update_tel =document.querySelector('#update_tel');
var btn_update_password =document.querySelector('#update_password');
btn_submitImage.addEventListener('click', ()=>{
    input_imageFile.click();
});
input_imageFile.addEventListener('change', function() {
    if(this.files.length){
        let file = this.files[0];
        let reader = new FileReader();
        reader.onload = function(e){
            image_portrait.src=e.target.result;
        };
        console.log(reader.result);
        reader.readAsDataURL(file);
    }
}, false);
image_portrait.addEventListener('load',function () {
    var changedPortrait = image_portrait.src;
    $.ajax({
        url:"/userInfo/portrait",
        type:"GET",
        data:{
            portrait: changedPortrait,
        },
        resultType:"JSON",
        success:function (result){
            if (result.code == 200) {
                // alert(result.message);
            } else {
                alert(result.message);
            }
        },
        error:function (result){
            alert(result.message);
        }
    });
});

btn_update_info.addEventListener('click',function () {
    $.ajax({
        url:"/userInfo/Info",
        type:"GET",
        data:{
            info_email: $("#info_email").val(),
            info_org: $("#info_org").val(),
            info_profile: $("#info_profile").val()
        },
        resultType:"JSON",
        success:function (result){
            if (result.code == 200) {
                alert(result.message);
            } else {
                alert(result.message);
            }
        },
        error:function (result){
            alert(result.message);
        }
    });
});

btn_update_tel.addEventListener('click',function () {
    $.ajax({
        url:"/userInfo/tel",
        type:"GET",
        data:{
            info_tel: $("#info_new_tel").val(),
        },
        resultType:"JSON",
        success:function (result){
            if (result.code === 200) {
                alert(result.message);
            } else {
                alert(result.message);
            }
        },
        error:function (result){
            alert(result.message);
        }
    });
});

btn_update_password.addEventListener('click',function () {
    console.log("abcdefh");
    $.ajax({
        url:"/userInfo/pwd",
        type:"GET",
        data:{
            old_password: $("#info_old_password").val(),
            new_password: $("#info_new_password").val(),
            check_password: $("#info_check_password").val(),
        },
        resultType:"JSON",
        success:function (result){

            if (result.code === 200) {
                alert(result.message);
                alert("账号信息已过期，请重新登录!");
                window.location.href="/exit";
            } else {
                alert(result.message);
            }
        },
        error:function (result){
            alert(result.message);
        }
    });
});



